package com.fiap.functions;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.fiap.dao.UserOperations;
import com.fiap.json.JSONParser;
import com.fiap.model.User;
import com.fiap.sqlite.DatabaseHelper;

public class UserFunctions {

	private JSONParser jsonParser;

	private static final String hostAddress = "10.0.2.2";
	private static final String applicationName = "phpapplicationbridge";
	private static final String loginURL = "http://" + hostAddress + "/" + applicationName  +"/";
	private static final String registerURL = "http://" + hostAddress + "/" + applicationName  +"/";
	private static final String postURL = "http://" + hostAddress + "/" + applicationName  +"/";

	private static String TAG_LOGIN_EMAIL = "login";
	private static String TAG_LOGIN_ID = "login_id";
	private static String TAG_REGISTER = "register";
	private static String TAG_INITIAL_INFO = "initial_info";
	private static String TAG_POST_ACTIVITY = "post_activity";

	public UserFunctions() {
		jsonParser = new JSONParser();
	}

	public JSONObject loginUser(String email, String password) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", TAG_LOGIN_EMAIL));
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("password", password));
		JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
		// return json
		Log.e("JSON", json.toString());
		return json;
	}

	public JSONObject loginUser(int userId) {

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", TAG_LOGIN_ID));
		params.add(new BasicNameValuePair("id", String.valueOf(userId)));
		JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
		// return json
		Log.e("JSON", json.toString());
		return json;
	}

	/**
	 * function make Login Request
	 * 
	 * @param name
	 * @param email
	 * @param password
	 * */

	public JSONObject registerUser(String name, String email, String password,
			String nickname) {
		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", TAG_REGISTER));
		params.add(new BasicNameValuePair("fullname", name));
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("password", password));
		params.add(new BasicNameValuePair("nickname", nickname));

		// getting JSON Object
		JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
		// return json
		return json;
	}

	public JSONObject updateInitialInfo(String sex, double weight,
			double height, String dateBirthday, String email, int id) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", TAG_INITIAL_INFO));
		params.add(new BasicNameValuePair("sex", sex));
		params.add(new BasicNameValuePair("weight", String.valueOf(weight)));
		params.add(new BasicNameValuePair("height", String.valueOf(height)));
		params.add(new BasicNameValuePair("dateBirthday", dateBirthday));
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("id", String.valueOf(id)));

		JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
		return json;
	}

	public JSONObject postOnRunners(int id, String message) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", TAG_POST_ACTIVITY));
		params.add(new BasicNameValuePair("message", message));
		params.add(new BasicNameValuePair("iduser", String.valueOf(id)));

		JSONObject json = jsonParser.getJSONFromUrl(postURL, params);
		return json;
	}

	/**
	 * Function get Login status
	 * */
	public boolean isUserLoggedIn(Context context) {
		UserOperations userOperations = new UserOperations(context);
		int count = userOperations.getRowCount();
		if (count > 0) {
			return true;
		}
		return false;
	}

	/**
	 * Function to logout user Reset Database
	 * */
//	public boolean logoutUser(Context context) {
//		DatabaseHelper db = new DatabaseHelper(context);
//		db.resetTables();
//		return true;
//	}

	public User getUser(Context context) {
		UserOperations userOperations = new UserOperations(context);
//		return userOperations.getUser();
		return null;
	}

}